home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Report Writers / Crystal Repot 9.0 Full CD version / Setup.exe / SRC / HOARDDLL.ZIP / 3rdParty / hoard / libhoard-2.0.2 / superblock.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-06-18  |  5.3 KB  |  154 lines

  1. ///-*-C++-*-//////////////////////////////////////////////////////////////////
  2. //
  3. // The Hoard Multiprocessor Memory Allocator
  4. // Contact author: Emery Berger, http://www.cs.utexas.edu/users/emery
  5. //
  6. // Copyright (c) 1998-2000, The University of Texas at Austin.
  7. //
  8. // This library is free software; you can redistribute it and/or modify
  9. // it under the terms of the GNU Library General Public License as
  10. // published by the Free Software Foundation, http://www.fsf.org.
  11. //
  12. // This library is distributed in the hope that it will be useful, but
  13. // WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. // Library General Public License for more details.
  16. //
  17. //////////////////////////////////////////////////////////////////////////////
  18.  
  19.  
  20. //////////////////////////////////////////////////////////////////////////////
  21. //
  22. // Note: This file was modified by Crystal Decisions in June 2002.
  23. //
  24. //////////////////////////////////////////////////////////////////////////////
  25.  
  26.  
  27. /*
  28.   superblock.cpp
  29.   ------------------------------------------------------------------------
  30.   The superblock class controls a number of blocks (which are
  31.   allocatable units of memory).
  32.   ------------------------------------------------------------------------
  33.   @(#) $Id: superblock.cpp,v 1.40 2000/03/21 18:19:55 emery Exp $
  34.   ------------------------------------------------------------------------
  35.   Emery Berger                    | <http://www.cs.utexas.edu/users/emery>
  36.   Department of Computer Sciences |             <http://www.cs.utexas.edu>
  37.   University of Texas at Austin   |                <http://www.utexas.edu>
  38.   ========================================================================
  39. */
  40.  
  41. #include <new.h>
  42. #include <string.h>
  43.  
  44. #include "arch-specific.h"
  45. #include "config.h"
  46. #include "heap.h"
  47. #include "processheap.h"
  48. #include "superblock.h"
  49.  
  50.  
  51. superblock::superblock (int numBlocks,    // The number of blocks in the sb.
  52.             int szclass,    // The size class of the blocks.
  53.             hoardHeap * o)    // The heap that "owns" this sb.
  54.   :
  55. #if HEAP_DEBUG
  56.     _magic (SUPERBLOCK_MAGIC),
  57. #endif
  58.     _sizeClass (szclass),
  59.     _numBlocks (numBlocks),
  60.     _numAvailable (0),
  61.     _fullness (0),
  62.     _freeList (NULL),
  63.     _owner (o),
  64.     _next (NULL),
  65.     _prev (NULL)
  66. {
  67.   assert (_numBlocks >= 1);
  68.  
  69.   // Determine the size of each block.
  70.   const int blksize =
  71.     hoardHeap::align (sizeof(block) + hoardHeap::sizeFromClass(_sizeClass));
  72.  
  73.   // Make sure this size is in fact aligned.
  74.   assert ((blksize & hoardHeap::ALIGNMENT_MASK) == 0);
  75.  
  76. #ifdef CRYSTAL_HOARD
  77.   _superblockClass = hoardHeap::superblockClass(_sizeClass);
  78.   assert ( _superblockClass >= 0 );
  79. #endif
  80.  
  81.   // Set the first block to just past this superblock header.
  82.   block * b
  83.     = (block *) hoardHeap::align ((unsigned long) (this + 1));
  84.  
  85.   // Initialize all the blocks,
  86.   // and insert the block pointers into the linked list.
  87.   for (int i = 0; i < _numBlocks; i++) {
  88.     // Make sure the block is on a double-word boundary.
  89.     assert (((unsigned int) b & hoardHeap::ALIGNMENT_MASK) == 0);
  90.     new (b) block (this);
  91.     assert (b->getSuperblock() == this);
  92.     b->setNext (_freeList);
  93.     _freeList = b;
  94.     b = (block *) ((char *) b + blksize);
  95.   }
  96.   _numAvailable = _numBlocks;
  97.   computeFullness();
  98.   assert ((unsigned long) b <= hoardHeap::align (sizeof(superblock) + blksize * _numBlocks) + (unsigned long) this);
  99.  
  100.   hoardLockInit (_upLock);
  101. }
  102.  
  103. superblock * superblock::makeSuperblock (int sizeclass,
  104.                      processHeap * pHeap)
  105. {
  106.   // We need to get more memory.
  107.  
  108.   char * buf;
  109.   int numBlocks = hoardHeap::numBlocks(sizeclass);
  110.  
  111.   // Compute how much memory we need.
  112.   unsigned long moreMemory;
  113.   if (numBlocks > 1) {
  114. #ifndef CRYSTAL_HOARD
  115.     moreMemory = hoardHeap::SUPERBLOCK_SIZE;
  116.     assert (moreMemory >= hoardHeap::align(sizeof(superblock) + (hoardHeap::align (sizeof(block) + hoardHeap::sizeFromClass(sizeclass))) * numBlocks));
  117.  
  118.     // Get some memory from the process heap.
  119.     buf = (char *) pHeap->getSuperblockBuffer();
  120. #else    
  121.     int superblockClass = hoardHeap::superblockClass(sizeclass);
  122.     moreMemory = hoardHeap::superblockSizeFromClass(superblockClass);
  123.     assert (moreMemory >= hoardHeap::align(sizeof(superblock) + (hoardHeap::align (sizeof(block) + hoardHeap::sizeFromClass(sizeclass))) * numBlocks));
  124.  
  125.     // Get some memory from the process heap.
  126.     buf = (char *) pHeap->getSuperblockBuffer(superblockClass);
  127. #endif  // CRYSTAL_HOARD
  128.   } else {
  129.     // One object.
  130.     assert (numBlocks == 1);
  131.  
  132.     size_t blksize = hoardHeap::align (sizeof(block) + hoardHeap::sizeFromClass(sizeclass));
  133.     moreMemory = hoardHeap::align (sizeof(superblock) + blksize);
  134.  
  135.     // Get space from the system.
  136.     buf = (char *) hoardSbrk (moreMemory);
  137.   }
  138.  
  139.   // Make sure that we actually got the memory.
  140.   if (buf == NULL) {
  141.     return 0;
  142.   }
  143.   buf = (char *) hoardHeap::align ((unsigned long) buf);
  144.  
  145.   // Make sure this buffer is double-word aligned.
  146.   assert (buf == (char *) hoardHeap::align ((unsigned long) buf));
  147.   assert ((((unsigned long) buf) & hoardHeap::ALIGNMENT_MASK) == 0);
  148.  
  149.   // Instantiate the new superblock in the buffer.
  150.   superblock * sb = new (buf) superblock (numBlocks, sizeclass, NULL);
  151.  
  152.   return sb;
  153. }
  154.